package com.hsd.core.page.base;

/**
 * <p>
 * 类描述：简单分页类
 * <p>
 * 创建人：Abdi
 * <p>
 * 创建时间：2013-11-2 下午04:44:35
 */
public class SimplePage implements Paginable {
	private static final long serialVersionUID = 1L;
	public static final int DEF_COUNT = 20;
	protected int totalCount = 0;
	protected int pageSize = 10;
	protected int pageNo = 1;
	private String operate;
	
	/**
	 * 检查页码 checkPageNo
	 * 
	 * @param pageNo
	 * @return if pageNo==null or pageNo<1 then return 1 else return pageNo
	 */
	public static int cpn(Integer pageNo) {
		return (pageNo == null || pageNo < 1) ? 1 : pageNo;
	}

	public SimplePage() {
	}

	/**
	 * 构造器
	 * 
	 * @param pageNo
	 *            页码
	 * @param pageSize
	 *            每页几条数据
	 * @param totalCount
	 *            总共几条数据
	 */
	public SimplePage(int pageNo, int pageSize, int totalCount) {
		setTotalCount(totalCount);
		setPageSize(pageSize);
		setPageNo(pageNo);
		adjustPageNo();
		setOperate(genOperate());
	}
	
	/**
	 * 分页操作栏
	 * @return
	 */
	public String genOperate(){
		StringBuilder operate = new StringBuilder();
		
		if(this.getTotalCount() == 0){
			operate.append("<div class=\"page-wrap\">");
			operate.append("<input id=\"_pagesize\" type=\"hidden\" value=\"" + getPageSize() + "\">");
			operate.append("</div>");
			return operate.toString();
		}
		
		int beginPage = (this.pageNo - 1) / 5 * 5 + 1;
		int endPage = (beginPage + 4) <= this.getTotalPage() ? (beginPage + 4) : this.getTotalPage();
		
		operate.append("<div class=\"page-wrap\">");
		
		operate.append("<div class=\"pull-left\">");
		operate.append("找到 <strong>" + this.totalCount + "</strong> 条结果");
		operate.append("</div>");
		
		operate.append("<ul class=\"page\">");
		if(this.getPageNo() == 1){
			operate.append("<li class=\"disable\">||</li>");
			operate.append("<li class=\"disable\">《</li>");
		}else{
			operate.append("<li onclick=\"Page.goPage(1)\">||</li>");
			operate.append("<li onclick=\"Page.goPage("+this.getPrePage()+")\">《</li>");
		}
		for(int pageIndex = beginPage; pageIndex <= endPage; pageIndex++){
			operate.append("<li");
			if(this.getPageNo() == pageIndex ){
				operate.append(" class=\"active\"");
			}else {
				operate.append(" onclick=\"Page.goPage("+pageIndex+")\"");
			}
			operate.append(">" + pageIndex + "</li>");
		}
		if(this.getTotalPage() > 5 && endPage < this.getTotalPage()){
			operate.append("<li onclick=\"Page.goPage(" + (endPage + 1) +")\">…</li>");
		}
		
		if(this.getPageNo() == this.getTotalPage()){
			operate.append("<li class=\"disable\">》</li>");
			operate.append("<li class=\"disable\">||</li>");
		}else{
			operate.append("<li onclick=\"Page.goPage("+this.getNextPage()+")\">》</li>");
			operate.append("<li onclick=\"Page.goPage("+this.getTotalPage()+")\">||</li>");
		}
		operate.append("</ul>");
		
		operate.append("<select class=\"pull-right\" onchange=\"Page.goPage(options[this.options.selectedIndex].value)\">");
		for(int pageIndex = 1; pageIndex <= this.getTotalPage(); pageIndex++){
			operate.append("<option value=\"" + pageIndex + "\"");
			if(this.getPageNo() == pageIndex ){
				operate.append(" selected==\"selected\"");
			}else{
				operate.append(" ");
			}
			operate.append(">" + pageIndex + "/" + this.getTotalPage() + "</option>");
		}
		operate.append("</select>");
		operate.append("<input id=\"_pagesize\" type=\"hidden\" value=\"" + getPageSize() + "\">");
		operate.append("</div>");
		return operate.toString();
	}
	
	/**
	 * 调整页码，使不超过最大页数
	 */
	public void adjustPageNo() {
		if (pageNo == 1) {
			return;
		}
		int tp = getTotalPage();
		if (pageNo > tp) {
			pageNo = tp;
		}
	}

	/**
	 * 获得页码
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 每页几条数据
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 总共几条数据
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * 总共几页
	 */
	public int getTotalPage() {
		int totalPage = totalCount / pageSize;
		if (totalPage == 0 || totalCount % pageSize != 0) {
			totalPage++;
		}
		return totalPage;
	}

	/**
	 * 是否第一页
	 */
	public boolean isFirstPage() {
		return pageNo <= 1;
	}

	/**
	 * 是否最后一页
	 */
	public boolean isLastPage() {
		return pageNo >= getTotalPage();
	}

	/**
	 * 下一页页码
	 */
	public int getNextPage() {
		if (isLastPage()) {
			return pageNo;
		} else {
			return pageNo + 1;
		}
	}

	/**
	 * 上一页页码
	 */
	public int getPrePage() {
		if (isFirstPage()) {
			return pageNo;
		} else {
			return pageNo - 1;
		}
	}

	/**
	 * if totalCount<0 then totalCount=0
	 * 
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		if (totalCount < 0) {
			this.totalCount = 0;
		} else {
			this.totalCount = totalCount;
		}
	}

	/**
	 * if pageSize< 1 then pageSize=DEF_COUNT
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		if (pageSize < 1) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
	}

	/**
	 * if pageNo < 1 then pageNo=1
	 * 
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		if (pageNo < 1) {
			this.pageNo = 1;
		} else {
			this.pageNo = pageNo;
		}
	}
	

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}
}
